/**
 * copyright 2014 by luobiliu
 *
 * @file:       PunchSettingData.cpp
 * @author:     luobiliu
 * @version:    1.0
 * @date:       2014-8-4
 *
 */

#include "PunchSettingData.h"
#include "PunchStaticData.h"
USING_NS_CC;
static PunchSettingData* _sharedPunchSettingData = NULL;
PunchSettingData* PunchSettingData::sharedSettingData()
{
    if(_sharedPunchSettingData==NULL){
        _sharedPunchSettingData = new PunchSettingData();
        _sharedPunchSettingData->init();
    }
    return _sharedPunchSettingData;
}
void PunchSettingData::purge()
{
    CC_SAFE_RELEASE_NULL(_sharedPunchSettingData);
}
PunchSettingData::PunchSettingData()
{
    
}
PunchSettingData::~PunchSettingData()
{
    this->flush();
}
bool PunchSettingData::init()
{
    _isBeginner = CCUserDefault::sharedUserDefault()->getBoolForKey("beginner",true);
    if(_isBeginner == true){
        this->reset();
        this->flush();
        this->setIsBeginner(false);
    }else{
        _isBeginner = CCUserDefault::sharedUserDefault()->getBoolForKey("beginner");
        _soundVolume = CCUserDefault::sharedUserDefault()->getFloatForKey("sound");
        _musicVolume = CCUserDefault::sharedUserDefault()->getFloatForKey("music");
        _gold = CCUserDefault::sharedUserDefault()->getIntegerForKey("gold");
        CCUserDefault::sharedUserDefault()->purgeSharedUserDefault();
    }
    return true;
}
void PunchSettingData::reset()
{
    int gold = STATIC_DATA_INT("default_gold");
    this->setGold(gold);
    this->setIsBeginner(true);
    this->setMusicVolume(1);
    this->setSoundVolume(1);
    this->flush();
}
void PunchSettingData::alterGold(int delta)
{
    this->setGold(this->getGold()+delta);
}
void PunchSettingData::flush()
{
    CCUserDefault::sharedUserDefault()->setFloatForKey("sound", this->getSoundVolume());
    CCUserDefault::sharedUserDefault()->setBoolForKey("beginner", this->getIsBeginner());
    CCUserDefault::sharedUserDefault()->setIntegerForKey("gold", this->getGold());
    CCUserDefault::sharedUserDefault()->setFloatForKey("music", this->getMusicVolume());
    CCUserDefault::sharedUserDefault()->flush();
    CCUserDefault::sharedUserDefault()->purgeSharedUserDefault();
}



